﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Member;
using WuDada.Core.Member.Service;
using WuDada.Core.SystemApplications.Domain;
using WuDada.Core.Member.Domain;
using System.Text;
using Common.Logging;

public partial class admin_UC06_0601 : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private MemberFactory m_MemberFactory;
    private IMemberService m_MemberService;
    private WebLogService m_WebLogService;

    private int m_Mode
    {
        get { if (ViewState["mode"] == null) { ViewState["mode"] = 0; } return int.Parse(ViewState["mode"].ToString()); }
        set { ViewState["mode"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        m_MemberFactory = new MemberFactory();
        m_MemberService = m_MemberFactory.GetMemberService();
        m_WebLogService = new WebLogService();

        if (!Page.IsPostBack)
        {
            pnlContent.Visible = false;
            fillGridView();
            ShowMode();
        }
    }

    private void ShowMode()
    {
        if (m_Mode == 0)
        {
            btnSave.Visible = false;
        }
        else
        {
            btnSave.Visible = true;
        }
    }

    protected void btnQuery_Click(object sender, ImageClickEventArgs e)
    {
        fillGridView();
    }

    private void fillGridView()
    {
        StringBuilder sbHql = new StringBuilder();
        string flag = ddlSearchFlag.SelectedValue;
        if (!string.IsNullOrEmpty(flag))
        {
            sbHql.Append(string.Format(" WHERE Flag = {0} ", flag));
        }
        else
        {
            sbHql.Append(" WHERE Flag >= 0 ");
        }

        //搜尋條件
        string name = txtSearchName.Text.Trim();
        if (!string.IsNullOrEmpty(name))
        {
            sbHql.Append(string.Format(@" AND Name like '%{0}%' ", name));
        }

        m_Log.Error("sbHql = " + sbHql.ToString());

        //分頁
        AspNetPager1.RecordCount = m_MemberService.CountMemberByWhere(sbHql.ToString());
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;

        sbHql.Append(" ORDER BY Name ");
        gvDetail.DataSource = m_MemberService.SearchMemberByWhere(sbHql.ToString(), pageIndex, pageSize);
        gvDetail.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        fillGridView();
    }

    protected void gvDetail_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        int memberId = int.Parse(e.CommandArgument.ToString());
        MemberVO memberVO = m_MemberService.GetMemberById(memberId);
        switch (cmdName)
        {
            case "myModify":
                ClearUI();
                m_Mode = memberId;
                UIHelper.FillUI(pnlContent, memberVO);
                ShowMode();
                pnlContent.Visible = true;
                break;
            default:
                break;
        }
        fillGridView();
    }


    protected void gvDetail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            Control ctrl = e.Row;
            MemberVO memberVO = (MemberVO)e.Row.DataItem;
            if (memberVO.Flag == 1)
            {
                UIHelper.SetLabelText(ref ctrl, "lblFlag", "V");
            }
        }
    }

    private void ClearUI()
    {
        m_Mode = 0;
        UIHelper.ClearUI(pnlContent);
        pnlContent.Visible = false;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        MemberVO memberVO = m_MemberService.GetMemberById(m_Mode);
        //UIHelper.FillVO(pnlContent, memberVO);
        memberVO.Flag = int.Parse(ddlFlag.SelectedValue);
        m_MemberService.UpdateMember(memberVO);
        m_WebLogService.AddSystemLog(MsgVO.Action.修改, memberVO);
        fillGridView();
        ClearUI();
        ShowMode();
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ClearUI();
        ShowMode();
    }
}